<?

$check_me_authed = "NO_OK";	

if(($jid = $_POST["radioview"]) || ($jid = $_POST["job_id_posted"]) ){
	
	unset($COracle);
	
	$COracle = new oracleDb;
  $COracle->init($_SESSION["ORCL"], 
                 $_SESSION["OPASS"],
                 $_SESSION["SID"],
                 _NO_COMMENT);
  
	if ($COracle->connect()) {
		
		$srcDbMstCategory   = "MST_MODEL_CAT";
    $srcDbMstGroup      = "MST_MODEL_GRP";
  	$srcDbHdr			      = "PCH_RECV_HDR";
    $srcDbHdrLog	      = "LOG_PCH_RECV_HDR";
    $srcDbDtl           = "PCH_RECV_DTL";
		
		$_SESSION["jid"]  = $_POST["radioview"];
		$jobId            = &$_SESSION["jid"];
		
		/**
		* Fetch global header
		* And check the status if it is valid
		* where STATUS = _NOT_ACTIVE
		*
		*/
		$CFetchers->fetchLstDataHeader($COracle,
                                   $jobId,
                                   $srcDbHdr,
                                   $nHeader,
                                   $lstHeader);
                       
		if($lstHeader["STATUS"][0] == _NOT_ACTIVE ||
		   $lstHeader["STATUS"][0] == _CANCELED){
  		
  		/**
  		* Init the header vars
  		*
  		*/
  		$_POST["trDateEnt"]   = date("d-m-Y",
  		                             strtotime($lstHeader["TR_DATE"][0]));
  		$_POST["supplierEnt"] = $lstHeader["SUPPLIER"][0];
  		$_POST["currencyEnt"] = $lstHeader["CURRENCY"][0];
  		$_POST["rateEnt"]     = $lstHeader["RATE"][0];
  		$_POST["memoEnt"]     = $lstHeader["MEMO"][0];
  		
  		/**
  		* Init the detail
  		*
  		*/
  		fetchLstDataDetail($COracle,
                         $jobId,
                         $srcDbDtl,
                         $nDetail,
                         $lstDetail);
  		
  		for($iRow = 0; $iRow < $nDetail; $iRow++){
  		  
  		  /**
  		  * Since category and group were kept as integer
  		  * we need to reextract the integer become name
  		  *
  		  */
  		  fetchCatById($COracle,
  		               $lstDetail["ICAT"][$iRow],
  		               $srcDbMstCategory,
  		               $_POST["catEnt"][$iRow]);
  		  
  		  fetchGrpById($COracle,
    		             $lstDetail["IGROUP"][$iRow],
    		             $srcDbMstGroup,
    		             $_POST["grpEnt"][$iRow]);
  		  
  		  /**
  		  * Init the normal vars
  		  *
  		  */
  		  $_POST["odrDateEnt"][$iRow] = $lstDetail["ORDER_DATE"][$iRow];
  		  $_POST["odrRefEnt"][$iRow]  = $lstDetail["ORDER_REF"][$iRow];
  		  $_POST["odrNoEnt"][$iRow]   = $lstDetail["ORDER_NO"][$iRow];
  		  $_POST["modelEnt"][$iRow]   = $lstDetail["MODEL"][$iRow];
  		  $_POST["modelIdEnt"][$iRow] = $lstDetail["MODEL_ID"][$iRow];
  		  $_POST["unitEnt"][$iRow]    = $lstDetail["UNIT"][$iRow];
  		  $_POST["qtyEnt"][$iRow]     = $lstDetail["QUANTITY"][$iRow];
  		  $_POST["cvtEnt"][$iRow]     = $lstDetail["CVT_TO"][$iRow];
  		  $_POST["cvtQtyEnt"][$iRow]  = $lstDetail["CVT_QTY"][$iRow];
  		  $_POST["recvQtyEnt"][$iRow] = $lstDetail["RECV_QTY"][$iRow];
  		  $_POST["priceEnt"][$iRow]   = $lstDetail["PRICE"][$iRow];
  		  $_POST["hrzTotEnt"][$iRow]  = $lstDetail["HRZ_TOT"][$iRow];
  		}
  		
  		/**
  		* Check if this tx isn't canceled
  		* YES: it is modify
  		* NO : Add new entry with this copy
  		*
  		*/
  		if($lstHeader["STATUS"][0] != _CANCELED){
  		  
  		  $_SESSION["modify_mode"]= "Y";
  		}else{
  		  
  		  $_SESSION["modify_mode"]= "N";
  		}
  		
  		/**
  		* This is only to by pass
  		* the entry_detail precheck
  		*
  		*/
  		//$_POST["entry"]         = "byPass";  
  		$_SESSION["init_num"]   = $nDetail;
  		
  		$COracle->dc();
  		
  		require("entry_restore_me.php");
  	
  	}else{
  	  
  	  usrDie("Tx can't be modified, it has been approved");
  	}
	}else{
	  
	  usrDie("Error: Oracle connection failed");
	}
}else{
	
	usrDie("Error:Cant retrieved job_id");
}

?>